read_ods("/datadisk/Current_share/_____latest/CE_data_from_20220831.ods", sheet = "Health") %>%
  select(Dates, `Waking pulse`, `Min pulse`) %>%
  rename(PulseRate = `Waking pulse`,
         minPR = `Min pulse`) %>%
    mutate(Date = as.Date(Dates, format = "%d/%m/%y"),
         PulseRate = as.numeric(PulseRate),
         PulseNA = is.na(PulseRate),
         minPR = as.numeric(minPR)) %>%
  ### only use the second block
  filter(Date > as.Date("01/01/2024", format = "%d/%m/%Y")) -> tibDat
## New names:
## • `` -> `...28`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `Time` -> `Time...33`
## • `Time` -> `Time...37`
## • `Time` -> `Time...40`
## • `Durn` -> `Durn...43`
## • `Dist` -> `Dist...44`
## • `MaxHR` -> `MaxHR...46`
## • `` -> `...51`
## • `Time` -> `Time...53`
## • `Temp` -> `Temp...57`
## • `Durn` -> `Durn...58`
## • `MaxHR` -> `MaxHR...63`
## • `Dist` -> `Dist...64`
## • `Temp` -> `Temp...74`
## • `Notes` -> `Notes...75`
## • `` -> `...76`
## • `Notes` -> `Notes...89`
## • `` -> `...90`
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `PulseRate = as.numeric(PulseRate)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
### now get the first and last dates with usable pulse rates
tibDat %>%
  filter(!is.na(minPR)) %>%
  summarise(first = min(Date),
            last = max(Date)) -> tmpTib

### and filter to within those dates
tibDat %>%
  filter(Date >= tmpTib$first & Date <= tmpTib$last) %>%
  mutate(dayN = row_number()) -> tibDat

### get summary statistics
tibDat %>%
  summarise(first = min(Date),
            last = max(Date),
            n = n(),
            PRnNA = getNNA(PulseRate),
            PRnOK = n - PRnNA,
            PRmin = min(PulseRate, na.rm = TRUE),
            PRmean = mean(PulseRate, na.rm = TRUE),
            PRmedian = median(PulseRate, na.rm = TRUE),
            PRmax = max(PulseRate, na.rm = TRUE),
            PRsd = sd(PulseRate, na.rm = TRUE),
            minPRnNA = getNNA(minPR),
            minPRnOK = n - minPRnNA,
            minPRmin = min(minPR, na.rm = TRUE),
            minPRmean = mean(minPR, na.rm = TRUE),
            minPRmedian = median(minPR, na.rm = TRUE),
            minPRmax = max(minPR, na.rm = TRUE),
            minPRsd = sd(minPR, na.rm = TRUE)) -> tibStats

tibStats %>%
  select(-c(first, last)) %>%
  pivot_longer(cols = everything(),
               names_to = "Statistic") %>%
  ### create grouping
  mutate(whichPR = if_else(str_detect(Statistic, fixed("minPR")),
                           "Min overnight PR",
                           "Waking PR"),
         whichPR = if_else(Statistic == "n", 
                           "Overall",
                           whichPR)) %>%
  as_grouped_data(groups = "whichPR") %>%
  flextable() %>%
  colformat_double(digits = 2) %>%
  autofit()

whichPR

Statistic

value

Overall

n

171.00

Waking PR

PRnNA

8.00

PRnOK

163.00

PRmin

53.00

PRmean

60.44

PRmedian

59.00

PRmax

79.00

PRsd

5.44

Min overnight PR

minPRnNA

7.00

minPRnOK

164.00

minPRmin

47.00

minPRmean

53.10

minPRmedian

53.00

minPRmax

62.00

minPRsd

2.44

ggplot(data = tibDat,
       aes(x = Date, y = PulseRate)) +
  geom_point() +
  geom_line() +
  geom_smooth() +
  geom_hline(yintercept = tibStats$PRmean,
             linetype = 3) +
  geom_linerange(data = filter(tibDat,
                               is.na(PulseRate)),
                 aes(x = Date,
                     ymin = -Inf,
                     ymax = Inf),
                     colour = "red") +
  ylab("Waking pulse rate") +
  scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y") +
  ggtitle("Waking pulse rates")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 8 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 8 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(data = tibDat,
       aes(x = Date, y = minPR)) +
  geom_point() +
  geom_line() +
  geom_smooth() +
  geom_hline(yintercept = tibStats$minPRmean,
             linetype = 3) +
  geom_linerange(data = filter(tibDat,
                               is.na(minPR)),
                 aes(x = Date,
                     ymin = -Inf,
                     ymax = Inf,
                     colour = "red")) +
  ylab("Minimum overnight pulse rate") +
  scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y") +
  ggtitle("Minimum overnight pulse rates")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 7 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).

x <- zoo(tibDat$PulseRate, tibDat$dayN)
x <- as.ts(x, start = 1, frequency = 1)
x <- na.interp(x)
acf(x)

ggtsdisplay(x,lag.max=30)

x <- zoo(tibDat$minPR, tibDat$dayN)
x <- as.ts(x)
x <- na.interp(x)
acf(x)

ggtsdisplay(x,lag.max=30)